Skip to content

Commit 7af9934

Browse files
committed
Add error messages for failed version lookups (when nodejs.org returns partial/no results. Resolves #1064.
1 parent 4854ee0 commit 7af9934

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

src/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/blang/semver v3.5.1+incompatible
99
github.com/coreybutler/go-fsutil v1.2.0
1010
github.com/coreybutler/go-where v1.0.2
11-
github.com/gonutz/w32/v2 v2.8.1
1211
github.com/olekukonko/tablewriter v0.0.5
1312
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d
1413
golang.org/x/sys v0.1.0

src/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ github.com/coreybutler/go-fsutil v1.2.0 h1:kbm62NSofawglUppEOhpHC3NDf/J7ZpguBirB
44
github.com/coreybutler/go-fsutil v1.2.0/go.mod h1:B+6ufEkkRZgFwyR2sHEVG9dMzVBU3GbyGyYmCq7YkEk=
55
github.com/coreybutler/go-where v1.0.2 h1:Omit67KeTtKpvSJjezVxnVD4qMtvlXDlItiKpVCdcl4=
66
github.com/coreybutler/go-where v1.0.2/go.mod h1:IqV4saJiDXdNJURfTfVRywDHvY1IG5F+GXb2kmnmEe8=
7-
github.com/gonutz/w32/v2 v2.8.1 h1:fTAzhg35iCGWqroImF18+A8GbI3MdXcTYuhvpBpd5CM=
8-
github.com/gonutz/w32/v2 v2.8.1/go.mod h1:MgtHx0AScDVNKyB+kjyPder4xIi3XAcHS6LDDU2DmdE=
97
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
108
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
119
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=

src/node/node.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package node
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io/ioutil"
67
"nvm/arch"
78
"nvm/file"
89
"nvm/web"
10+
"os"
911
"os/exec"
1012
"regexp"
1113
"strings"
12-
"os"
1314

1415
// "../semver"
1516
"github.com/blang/semver"
@@ -208,13 +209,20 @@ func GetAvailable() ([]string, []string, []string, []string, []string, map[strin
208209

209210
// Check the service to make sure the version is available
210211
text := web.GetRemoteTextFile(url)
212+
if len(text) == 0 {
213+
fmt.Println("Error retrieving version list: \"" + url + "\" returned blank results. This can happen when the remote file is being updated. Please try again in a few minutes.")
214+
os.Exit(0)
215+
}
211216

212217
// Parse
213218
var data = make([]map[string]interface{}, 0)
214-
json.Unmarshal([]byte(text), &data)
219+
err := json.Unmarshal([]byte(text), &data)
220+
if err != nil {
221+
fmt.Printf("Error retrieving versions from \"%s\": %v", url, err.Error())
222+
os.Exit(1)
223+
}
215224

216225
for _, element := range data {
217-
218226
var version = element["version"].(string)[1:]
219227
all = append(all, version)
220228

src/nvm.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,12 @@ func checkLocalEnvironment() {
11381138
}
11391139
} else {
11401140
if fileInfo.Mode()&os.ModeSymlink != 0 {
1141-
targetPath, _ := os.Readlink(symlink)
1142-
targetFileInfo, _ := os.Lstat(targetPath)
1141+
targetPath, err := os.Readlink(symlink)
1142+
if err != nil {
1143+
fmt.Println(err)
1144+
}
1145+
1146+
targetFileInfo, err := os.Lstat(targetPath)
11431147

11441148
if !targetFileInfo.Mode().IsDir() {
11451149
problems = append(problems, "NVM_SYMLINK is a symlink linking to a file instead of a directory.")
@@ -1312,7 +1316,14 @@ func getLatest() string {
13121316
}
13131317

13141318
func getLTS() string {
1315-
_, ltsList, _, _, _, _ := node.GetAvailable()
1319+
all, ltsList, current, stable, unstable, npm := node.GetAvailable()
1320+
fmt.Println(all)
1321+
fmt.Println(ltsList)
1322+
fmt.Println(current)
1323+
fmt.Println(stable)
1324+
fmt.Println(unstable)
1325+
fmt.Println(npm)
1326+
// _, ltsList, _, _, _, _ := node.GetAvailable()
13161327
// ltsList has already been numerically sorted
13171328
return ltsList[0]
13181329
}

src/web/web.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ func GetRemoteTextFile(url string) string {
321321
os.Exit(1)
322322
}
323323

324+
if response.StatusCode != 200 {
325+
fmt.Printf("Error retrieving \"%s\": HTTP Status %v\n", url, response.StatusCode)
326+
os.Exit(0)
327+
}
328+
324329
defer response.Body.Close()
325330

326331
contents, readerr := ioutil.ReadAll(response.Body)

0 commit comments

Comments
 (0)